#include "WiFly.h"
#include "pfe.h"

void create_adhoc(char * ssid, int chanel) {

  Serial.begin(9600);
  Serial.println("Creation d'un reseau adhoc");
  Serial.println("----------------------------------------");  
  Serial.println();

  Serial.print("Ssid : ");
  Serial.println(ssid);
  Serial.print("Chanel : ");
  Serial.println(chanel);
  Serial.println();

  Serial.println("Appuyez sur une touche pour continuer");

  while(!Serial.available()) {
  }

  Serial.println();

  // --------------------------

  Serial.println("Tentative de connection au bus serie");
  SpiSerial.begin();
  delay(1000);
  Serial.println("Connecte au bus SPI UART");
  Serial.println();
  delay(1000);
  // --------------------------

  Serial.println("Lancement de la configuration");
  Serial.println();
  delay(1000);

  Serial.println("Passage en mode commande");
  SpiSerial.print("$$$");
  delay(800);
  Serial.println("done.");
  Serial.println("");

  Serial.println("Configuration du mode wifi en adhoc");
  SpiSerial.println("set wlan join 4");
  delay(800);
  Serial.println("done.");
  Serial.println("");

  Serial.println("Configuration du ssid");
  SpiSerial.print("set wlan ssid ");
  SpiSerial.println(ssid);
  delay(800);
  Serial.println("done.");
  Serial.println("");

  Serial.println("Configuration du chanel");
  SpiSerial.print("set wlan chan ");
  SpiSerial.println(chanel);
  delay(800);
  Serial.println("done.");
  Serial.println("");

  Serial.println("Activation du DHCP");
  SpiSerial.println("set ip dhcp 2");
  delay(800);
  Serial.println("done.");
  Serial.println("");

  Serial.println("Sauvegarde de la configuration");
  SpiSerial.println("save");
  delay(800);
  Serial.println("done.");
  Serial.println("");

  Serial.println("Rebooting...");
  SpiSerial.println("reboot");
  delay(3000);
  Serial.println("");

  Serial.println("-- CONFIGURATION TERMINEE --");
  Serial.end();
}

int join_adhoc(char * ssid){
  Serial.begin(9600);
  Serial.println("Connection a un reseau adhoc");
  Serial.println("----------------------------------------");  
  Serial.println();

  Serial.print("Ssid : ");
  Serial.println(ssid);
  Serial.println();

  Serial.println("Appuyez sur une touche pour continuer");

  while(!Serial.available()) {
  }

  Serial.println();

  // --------------------------

  Serial.println("Tentative de connection au bus serie");
  SpiSerial.begin();
  delay(1000);
  Serial.println("Connecte au bus SPI UART");
  Serial.println();
  delay(1000);
  // --------------------------

  Serial.println("Passage en mode commande");
  SpiSerial.print("$$$");
  delay(800);
  Serial.println("done.");
  Serial.println("");

  Serial.println("Connection...");
  SpiSerial.println("leave");
  delay(100);
  SpiSerial.print("join ");
  SpiSerial.println(ssid);
  delay(3000);
  Serial.println("done.");
  Serial.println("");

  Serial.println("Sauvegarde de la configuration");
  SpiSerial.println("save");
  delay(800);
  Serial.println("done.");
  Serial.println("");

  Serial.println("Rebooting...");
  SpiSerial.println("reboot");
  delay(3000);
  Serial.println("");

  Serial.println("-- CONNECTION TERMINEE --");
  Serial.end();
}










